package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.entity.Address;
import com.entity.Consumer;
import com.servers.AddressServers;
import com.servers.ConsumerServers;

@Controller
public class AddressController {
	@Autowired
	private AddressServers addressServers;
	@Autowired
	private  ConsumerServers consumerserver;
	@RequestMapping("/getInsertAddressInfo")
	public String  getInsertAddressInfo(String addressName,String messageAddress,String messageTel,HttpServletRequest request){
		System.out.println("------进入address----------");
		//System.out.println(addressName+"=="+messageAddress+"==="+messageTel);
		Address address= new Address();
		int  cid=(int) request.getSession().getAttribute("CURR_CID");
		address.setAddress(messageAddress);
		address.setName(addressName);
		address.setTel(messageTel);
		address.setConsumer(new Consumer(cid));
		int num=addressServers.getIndexOfNewAddress(address);
		return "redirect:BookOrder";
	}
	@RequestMapping("/getAddressInfo")
	public ModelAndView getAddressInfo(HttpServletRequest request){
		System.out.println("============进入控制类=================");
		ModelAndView mv=new ModelAndView();
		int  cid=(int) request.getSession().getAttribute("CURR_CID");
		//int cid=1;
		List<Address> list=addressServers.getAddressInfo(cid);
		Consumer consumer=consumerserver.getConsumerInfoByCid(cid);	
		mv.addObject("address", list);
		mv.addObject("consumer",consumer);
		mv.setViewName("personAddress");
		return mv;
	}
	@RequestMapping("/getPersonInsertAddressInfo")
	public String  getInsertAddressPerson(String addressName,String messageAddress,String messageTel,HttpServletRequest request){
		System.out.println("------进入address----------");
		//System.out.println(addressName+"=="+messageAddress+"==="+messageTel);
		Address address= new Address();
		int  cid=(int) request.getSession().getAttribute("CURR_CID");
		//int cid=1;
		address.setAddress(messageAddress);
		address.setName(addressName);
		address.setTel(messageTel);
		address.setConsumer(new Consumer(cid));
		int num=addressServers.getIndexOfNewAddress(address);
		return "redirect:getAddressInfo";
	}
	@RequestMapping("/deleteAddressInfo")
	public String  deleteAddress(int aid){
		System.out.println("============地址主键:"+aid);
		int num =addressServers.deleteAddress(aid);
		System.out.println("删除后返回值:"+num);
		return "redirect:getAddressInfo";
	}
	@RequestMapping("/updateAddressInfo")
	public String  updateAddress(String sendAddress,String orderTel ,String orderCusomer ,int hiddenAid){
		System.out.println("===========进入更新地址======"+sendAddress+"===="+orderTel+"===="+orderCusomer+"======"+hiddenAid);
		Address address =new Address();
		address.setAid(hiddenAid);
		address.setName(orderCusomer);
		address.setTel(orderTel);
		address.setAddress(sendAddress);
		int num=addressServers.updateAddree(address);
		//System.out.println(num+"===============");	
		return "redirect:getAddressInfo";
	}
	public void setAddressServers(AddressServers addressServers) {
		this.addressServers = addressServers;
	}
	public void setConsumerserver(ConsumerServers consumerserver) {
		this.consumerserver = consumerserver;
	}
	
	
	
	
}
